package javabasics.concurrent.product_comsumer;

import java.util.concurrent.BlockingDeque;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingDeque;

/**
 * @Author suxiaoxiao
 * @Date 2019/6/16 15:38
 * @Version 1.0
 */
public class ProductComsumerTest {

    public static void main(String[] args) throws InterruptedException {
        // 建立缓冲区
        BlockingDeque<PCData> queue = new LinkedBlockingDeque<PCData>(10);
        // 三个生产者
        Producter product1 = new Producter(queue);
        Producter product2 = new Producter(queue);
        Producter product3 = new Producter(queue);
        // 三个消费者
        Consumer consumer1 = new Consumer(queue);
        Consumer consumer2 = new Consumer(queue);
        Consumer consumer3 = new Consumer(queue);

        ExecutorService service = Executors.newCachedThreadPool();

        service.execute(product1);
        service.execute(product2);
        service.execute(product3);

        service.execute(consumer1);
        service.execute(consumer2);
        service.execute(consumer3);

        Thread.sleep(10 * 1000);

        product1.stop();
        product2.stop();
        product3.stop();

        Thread.sleep(3000);
        service.shutdown();
    }

}
